package org.seoul.gtugs.client.presenter;

import java.util.List;

import org.seoul.gtugs.client.PhotoUploadServiceAsync;
import org.seoul.gtugs.client.ui.PhotoListDisplay;
import org.seoul.gtugs.shared.PhotoEntity;


import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.HasWidgets;


public class DesktopPhotoListPresenter implements Presenter{
	
	private final PhotoListDisplay display;
	private final PhotoUploadServiceAsync rpcService;
	
	public DesktopPhotoListPresenter(PhotoUploadServiceAsync rpcService, PhotoListDisplay display) {
		this.rpcService = rpcService;
		this.display = display;
	}	
	
	@Override
	public void go(HasWidgets container) {
		// TODO Auto-generated method stub
	    container.clear();
	    container.add(display.asWidget());
	    fetchGetPhotoList();

	}

	private void fetchGetPhotoList(){
		rpcService.listPhotos(new AsyncCallback<List<PhotoEntity>>() {
		      public void onSuccess(List<PhotoEntity> result) {
		          display.setData(result);
		        }
		        
		        public void onFailure(Throwable caught) {
		          Window.alert("Fail to execute the server! : " + caught.getMessage());
		        }
		      });
	}	
}
